package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.zxing.WriterException;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletAddCardContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletAddCardPresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.SystemUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.util.ZXingUtils;

import javax.inject.Inject;

public class WalletAddCardActivity extends BaseToolbarActivity implements WalletAddCardContract.View {

    @Inject
    WalletAddCardPresenter mPresenter;
    private ImageView mIvCode;
    private EditText mEtInput;
    private String stringExtra;
    private String temp_card_no;

    @Override
    protected void initView(Bundle savedInstanceState) {
        Intent intent = getIntent();
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);

        initId();
        initInfo();
        isEnable(false);
        stringExtra = intent.getStringExtra(Constants.ARGUMENT);
        setTbTitle(TextUtils.isEmpty(stringExtra) ? getString(R.string.wallet_cards_virtual_add) : getString(R.string.wallet_family_card));
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_add_card;
    }

    /**
     * 初始化Id
     */
    private void initId() {
        mIvCode = getViewById(R.id.iv_wallet_add_card_code);
        mEtInput = getViewById(R.id.et_wallet_add_card_name);
        RxUtils.bindEvents(getViewById(R.id.btn_wallet_add_card_confirm),this);
    }

    /**
     * 初始化数据
     */
    private void initInfo() {
        getCardCode();
    }

    private void getCardCode() {
        mPresenter.getCardCode();
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.btn_wallet_add_card_confirm:
                SystemUtils.hidKeyBoard(this);
                clickBtn();
                break;
        }
    }

    private void clickBtn() {
        if (stringExtra == null) {
            addTempCard();
        } else
            addNewCard();
    }

    private void addNewCard() {
        String name = mEtInput.getText().toString().trim();
        if (name == null)
            name = stringExtra;
        mPresenter.addNewCard(name, temp_card_no);
    }

    private void addTempCard() {
        String name = mEtInput.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_add_card_name_none));
            return;
        }
        if (temp_card_no == null) {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_add_card_waiting));
            return;
        }
        mPresenter.addTempCard(name, temp_card_no);
    }

    @Override
    public void showError(String msg) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void getCardCodeBack(boolean result, String msg) {
        if (!result) {
            temp_card_no = null;
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_add_card_get_error));
        } else {
            temp_card_no = msg;
            try {
                Bitmap qrCode = ZXingUtils.createQRCode(msg, 350);
                mIvCode.setImageBitmap(qrCode);
                Animation a = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
                mIvCode.setAnimation(a);
            } catch (WriterException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void addTempCardBack(boolean result, String msg) {
        if (result) {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_add_temp_card_success), 1);
            RxBus.get().post(new EventBean(Constants.EVENT_TYPE_FAMILY_CARD_UPDATE));
            finish();
        } else {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_add_card_create_error) + msg);
        }
    }

    @Override
    public void addCardBack(boolean result, String msg) {
        if (!result) {
            TipUtils.showToast(App.getInstance(), msg);
        } else {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_add_card_success), 1);
            RxBus.get().post(new EventBean(Constants.EVENT_TYPE_FAMILY_CARD_UPDATE));
            finish();
        }
    }

    @Override
    protected void onRefreshData() {

    }

}
